import java.util.List;

public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public void createList() {
        ListNode Node1 = new ListNode(1);
        ListNode Node2 = new ListNode(2);
        ListNode Node3 = new ListNode(3);
        ListNode Node4 = new ListNode(4);
        ListNode Node5 = new ListNode(5);
        this.head = Node1;
        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;
        Node4.next = Node5;
    }
    public ListNode head;
    public int useSize;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head.next = node;
        } else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode curNode = this.head;
        if (curNode == null) {
            this.head = node;
//            node.next = null;
        }else {
            while (curNode.next != null) {
                curNode = curNode.next;
            }
            curNode.next = node;
//            node.next = null;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        if (index < 0 || index > size()) {
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = searchPrev(index);
        node.next = cur.next;
        cur.next = node;
    }
    private ListNode  searchPrev(int index) {
        ListNode curNode = this.head;
        int cunt = 0;
        while (cunt != index-1) {
            curNode = curNode.next;
            cunt++;
        }
        return curNode;
    }

    @Override
    public int size() {
        int cunt = 0 ;
        ListNode curNode = this.head;
        while(curNode != null) {
            cunt++;
            curNode = curNode.next;
        }
        return cunt;
    }

    @Override
    public void clear() {
        ListNode curNode = this.head;
        while (curNode.next != null) {
            ListNode curNext = curNode.next;
            curNode.next = null;
            curNode = curNext;
        }
        head = null;
    }

    @Override
    public boolean contains(int key) {
        ListNode curNode = this.head;
        while (curNode != null) {
            if (curNode.val == key) {
                return true;
            }else {
                curNode = curNode.next;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode cur = findKey(key);
        if (cur == null) {
            System.out.println("没有你要的删除节点");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    private ListNode findKey(int key) {
        ListNode node = this.head;
        while (node.next != null) {
            if (node.next.val == key) {
                return node;
            }
            node = node.next;
        }
        return  null;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        ListNode pre = this.head;
        ListNode curNode = head.next;
        while(curNode != null) {
            if (curNode.val == key) {
                pre.next = curNode.next;
                curNode = curNode.next;
            } else {
                pre = curNode;
                curNode = curNode.next;
            }
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    @Override
    public void display() {
        ListNode curNode = this.head;
    while(curNode != null) {
        System.out.println(curNode.val);
        curNode = curNode.next;
    }
    }
}
